package com.atearsan.multiflavors.view.activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.atearsan.multiflavors.BuildConfig;
import com.atearsan.multiflavors.R;

public class MainActivity extends AppCompatActivity {
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        Button btnNext = (Button) findViewById(R.id.btnNext);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NextActivity.class);
                startActivity(intent);
            }
        });


        TextView tvBuildInfo = (TextView) findViewById(R.id.tvBuildInfo);
        StringBuilder sf = new StringBuilder();
        sf
                .append("Build信息").append("\r\n")
                .append("   APPLICATION_ID: ").append(BuildConfig.APPLICATION_ID).append("\r\n")
                .append("   BUILD_TYPE: ").append(BuildConfig.BUILD_TYPE).append("\r\n")
                .append("   FLAVOR: ").append(BuildConfig.FLAVOR).append("\r\n")
                .append("   API_HOST: ").append(BuildConfig.API_HOST).append("\r\n")
        ;
        tvBuildInfo.setText(sf.toString());


        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);

            TextView tvAPPInfo = (TextView) findViewById(R.id.tvAPPInfo);
            StringBuilder sfAPPInfo = new StringBuilder();
            sfAPPInfo
                    .append("APP信息").append("\r\n")
                    .append("   包名: ").append(pi.packageName).append("\r\n")
                    .append("   版本名: ").append(pi.versionName).append("\r\n")
                    .append("   版本号: ").append(pi.versionCode).append("\r\n")
            ;
            tvAPPInfo.setText(sfAPPInfo.toString());
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        try {
            ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);

            TextView tvAndroidManifestInfo = (TextView) findViewById(R.id.tvAndroidManifestInfo);
            StringBuilder sfAndroidManifestInfo = new StringBuilder();
            sfAndroidManifestInfo
                    .append("AndroidManifestInfo信息").append("\r\n")
                    .append("   XG_V2_ACCESS_ID: ").append(appInfo.metaData.getString("XG_V2_ACCESS_ID")).append("\r\n")
                    .append("   XG_V2_ACCESS_KEY: ").append(appInfo.metaData.getString("XG_V2_ACCESS_KEY")).append("\r\n")
            ;
            tvAndroidManifestInfo.setText(sfAndroidManifestInfo.toString());


        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }
}
